import Vue from 'vue'
import VueRouter from 'vue-router'
import notFound from "@/views/404.vue"
import Main from "@/views/main.vue"
import { getStorageUserInfo } from '@/utils'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
const publicRoutes = ['/login']
const routes = [
    {
        path: '/',
        name: 'homeParent',
        component: Main,
        redirect: '/home',
        meta: { permissions: [], title: '主页' },
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('../views/home.vue'),
                meta: { permissions: [], title: '主页' },
            }
        ]
    },

    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login.vue'),
    },
    {
        path: '/404',
        name: 'Page404',
        component: notFound
    },
    {
        path: '*',
        redirect: '/404'
    },
]

export const operatorRoutes = [
    {
        path: '/largeScreen',
        name: 'largeScreen',
        component: () => import('../views/largeScreen/largeScreen.vue'),
        icon: '\ue670',
        meta: { permissions: ['dataLargeScreen'], title: '数据大屏' },
    },
    {
        path: '/demand',
        name: 'demand',
        component: Main,
        icon: '\ue741',
        meta: { permissions: ['permissionDemand', 'admin'], title: '需求管理' },
        children: [
            {
                path: '/demandIndex',
                name: 'demandIndex',
                component: () => import('../views/demand/index.vue'),
                meta: { permissions: ['permissionDemand', 'admin'], title: '我的需求' },
            },
            {
                path: '/demandDocking',
                name: 'demandDocking',
                component: () => import('../views/demand/docking.vue'),
                meta: { permissions: ['permissionDemand', 'admin'], title: '成果对接' },
            },
        ],
    },
    {
        path: '/achievement',
        name: 'achievement',
        component: Main,
        icon: '\ue738',
        meta: { permissions: ['perimissionAch', 'admin'], title: '成果管理' },
        children: [
            {
                path: '/achievementIndex',
                name: 'achievementIndex',
                component: () => import('../views/achievement/index.vue'),
                meta: { permissions: ['perimissionAch', 'admin'], title: '我的成果' },
            },
            {
                path: '/achievementDocking',
                name: 'achievementDocking',
                component: () => import('../views/achievement/docking.vue'),
                meta: { permissions: ['perimissionAch', 'admin'], title: '需求对接' },
            },
        ],
    },
    
    {
        path: '/myCommission',
        name: 'myCommission',
        component: Main,
        icon: '\ue603',
        meta: { permissions: ['technical'], title: '我的委托' },
        children: [
            {
                path: '/myCommissionIndex',
                name: 'myCommissionIndex',
                component: () => import('../views/myCommission/index.vue'),
                meta: { permissions: [], title: '我的委托' },
            },
        ],
    },
    {
        path: '/auth',
        name: 'auth',
        component: Main,
        icon: '\ue62a',
        meta: { permissions: ['auditManagementDetail'], title: '审核管理' },
        children: [
            {
                path: '/authIndex',
                name: 'authIndex',
                component: () => import('../views/auth/index.vue'),
                meta: { permissions: [], title: '审核管理' },
            },
        ],
    },
    {
        path: '/userAuth',
        name: 'userAuth',
        component: Main,
        icon: '\ue639',
        meta: { permissions: ['personnelAudit'], title: '人员审核' },
        children: [
            {
                path: '/userAuthIndex',
                name: 'userAuthIndex',
                component: () => import('../views/userAuth/index.vue'),
                meta: { permissions: [], title: '人员审核' },
            },
        ],
    },
    {
        path: '/dockingAuth',
        name: 'dockingAuth',
        component: Main,
        icon: '\ue62a',
        meta: { permissions: ['abutmentAudit'], title: '对接审核' },
        children: [
            {
                path: '/dockingAuthIndex',
                name: 'dockingAuthIndex',
                component: () => import('../views/dockingAuth/index.vue'),
                meta: { permissions: [], title: '对接审核' },
            },
        ],
    },
    {
        path: '/announcement',
        name: 'announcement',
        component: Main,
        icon: '\ue60f',
        meta: { permissions: ['achievementPublicity','perimissionAch'], title: '成果公示' },
        children: [
            {
                path: '/announcementIndex',
                name: 'announcementIndex',
                component: () => import('../views/announcement/index.vue'),
                meta: { permissions: [], title: '成果公示' },
            },
        ],
    },
    {
        path: '/system',
        name: 'system',
        component: Main,
        icon: '\ue643',
        meta: { permissions: ['basicsConfiguration','resourceAbutment','bannerManagement','staffManagement','authorityManagement','admin'], title: '信息配置' },
        children: [

            {
                path: '/systemRegister',
                name: 'systemRegister',
                component: () => import('../views/system/register/register.vue'),
                meta: { permissions: ['basicsConfiguration'], title: '基础配置' },
            },
            {
                path: '/systemResources',
                name: 'systemResources',
                component: () => import('../views/system/resources/resources.vue'),
                meta: { permissions: ['resourceAbutment'], title: '资源对接' },
            },
            {
                path: '/systemBanner',
                name: 'systemBanner',
                component: () => import('../views/system/banner/index.vue'),
                meta: { permissions: ['bannerManagement'], title: 'banner管理' },
            },
            {
                path: '/staffMgt',
                name: 'staffMgt',
                component: () => import('../views/system/staffMgt/index.vue'),
                meta: { permissions: ['staffManagement'], title: '人员管理' },
            },
            {
                path: '/permissionMgt',
                name: 'permissionMgt',
                component: () => import('../views/system/permissionMgt/index.vue'),
                meta: { permissions: ['authorityManagement'], title: '权限管理' },
            },
            {
                path: '/systemLog',
                name: 'systemLog',
                component: () => import('../views/system/systemLog/index.vue'),
                meta: { permissions: ['admin'], title: '系统日志' },
            },
            {
                path: '/knowledge',
                name: 'knowledge',
                component: () => import('../views/system/knowledge/index.vue'),
                meta: { permissions: ['admin'], title: '知识库管理' },
            },
        ],
    }
]


const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes: routes
})

router.beforeEach((to, from, next) => {
    let userInfo = getStorageUserInfo()

    if (userInfo && userInfo.id) {
        //已登录
        next()
    } else if (publicRoutes.indexOf(to.path) !== -1) {
        //公开路由
        next()
    } else {
        //
        next('/login')
    }

});

router.afterEach(route => {
    //
});
export const setRouter = (callback) => {
    const list = generateRoutes()
    for (let r of list) {
        router.addRoute(r)
    }
    callback()
}
const hasPermission = (permissions, route) => {
    if (route.meta && route.meta.permissions && route.meta.permissions.length > 0) {
        return permissions.some(permission => route.meta.permissions.includes(permission))
    } else {
        return true
    }
}
const filterAsyncRoutesWithPermissions = (routes, permissions) => {
    const res = []
    routes.forEach(route => {
        const r = { ...route }
        if (hasPermission(permissions, r)) {
            if (r.children) r.children = filterAsyncRoutesWithPermissions(r.children, permissions)
            res.push(r)
        }
    })
    return res
}
export const generateRoutes = () => {
    const userInfo = getStorageUserInfo()
    if (userInfo && userInfo.id) {
        let permissions = []
        if (userInfo.roleName === '管理员') {
            const userPermissions = userInfo.permissions
            permissions = ['permissionDemand', 'perimissionAch', 'technical','admin'].concat(userPermissions)
        } else if (userInfo.roleName === '后台') {
            const userPermissions = userInfo.permissions
            permissions = userPermissions

        } else if (userInfo.hasTechnicalRepresentative === 1) {
            // 经纪人
            permissions = ['permissionDemand', 'perimissionAch', 'technical']

        } else if (userInfo.roleName === '需求方') {
            permissions = ['permissionDemand']
        } else if (userInfo.roleName === '成果方') {
            permissions = ['perimissionAch']
        } else if (userInfo.roleName === '中介方') {
            permissions = ['permissionDemand', 'perimissionAch']
        }
        // userInfo.resourceChildren.map(item => item.name)
        return filterAsyncRoutesWithPermissions(operatorRoutes, permissions)
    }
    return []
}
export default router
